<?php

namespace app\admin\controller;

use think\Controller;

class Activity extends Controller
{
    public function index()
    {
        $list = db('shuanglian')->alias('a')
            ->where('jia_status', 0)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->order('a.id', 'desc')
            ->paginate(1, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function publish()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $whe = [
                'title' => $post['title'],
                'img' => $post['thumb'],
                'content' => $post['content'],
                'description' => $post['description'],
                'time' => time()
            ];
            $in = db('shuanglian')->insert($whe);
            if ($in) {
                $this->success('创建活动成功！');
            }
        }
        return $this->fetch();
    }
    public function audit()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if (false == db('shuanglian')->where('id', $id)->update(['status' => $status])) {
                    return $this->error('审核提交失败');
                } else {
                    return $this->success('审核提交成功', 'admin/activity/index');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function delete()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('shuanglian')->where('id', $id)->update(['jia_status' => 1]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function change()
    {
        $id = input("id");
        $list = db('shuanglian')->alias('a')
            ->where('a.id', $id)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->find();
        $this->assign('list', $list);
        if (request()->isAjax()) {
            $post = input('post.');
            if (empty($post['thumb'])) {
                $in = db('shuanglian')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $in = db('shuanglian')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'img' => $post['thumb'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
        return $this->fetch();
    }
    public function publici()
    {
        $list = db('gongyi')->alias('a')
            ->where('jia_status', 0)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->order('a.id', 'desc')
            ->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function pub()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $whe = [
                'title' => $post['title'],
                'img' => $post['thumb'],
                'content' => $post['content'],
                'description' => $post['description'],
                'time' => time()
            ];
            $in = db('gongyi')->insert($whe);
            if ($in) {
                return $this->success('创建活动成功！', 'admin/user/public');
            }
        }
        return $this->fetch();
    }
    public function aud()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if (false == db('gongyi')->where('id', $id)->update(['status' => $status])) {
                    return $this->error('审核提交失败');
                } else {
                    return $this->success('审核提交成功', 'admin/activity/index');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function del()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('gongyi')->where('id', $id)->update(['jia_status' => 1]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function chan()
    {
        $id = input("id");
        $list = db('gongyi')->alias('a')
            ->where('a.id', $id)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->find();
        $this->assign('list', $list);
        if (request()->isAjax()) {
            $post = input('post.');
            if (empty($post['thumb'])) {
                $in = db('gongyi')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $in = db('gongyi')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'img' => $post['thumb'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
        return $this->fetch();
    }
    public function love()
    {
        $list = db('user')->where('integral', '<>', 0)->order('integral', 'desc')->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function see()
    {
        $id = input('id');
        $list = db('huodong')->alias('a')
            ->where('c.id', $id)
            ->field('a.text,b.*,c.title')
            ->join('user b', 'a.yong_id = b.id')
            ->join('shuanglian c', 'a.huo_id = c.id')
            ->order('id', 'desc')
            ->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function polular()
    {
        $list = db('kepu')->alias('a')
            ->where('jia_status', 0)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->order('a.id', 'desc')
            ->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
    public function publishee()
    {
        if (request()->isAjax()) {
            $post = input('post.');
            $whe = [
                'title' => $post['title'],
                'img' => $post['thumb'],
                'content' => $post['content'],
                'description' => $post['description'],
                'time' => time()
            ];
            $in = db('kepu')->insert($whe);
            if ($in) {
                $this->success('创建活动成功！');
            }
        }
        return $this->fetch();
    }
    public function auditirt()
    {
        $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
        if ($id > 0) {
            if ($this->request->isPost()) {
                //是提交操作
                $post = $this->request->post();
                $status = $post['status'];
                if (false == db('kepu')->where('id', $id)->update(['status' => $status])) {
                    return $this->error('审核提交失败');
                } else {
                    return $this->success('审核提交成功', 'admin/activity/polular');
                }
            }
        } else {
            return $this->error('id不正确');
        }
    }
    public function deletekkk()
    {
        if (request()->isAjax()) {
            $id = input('id');
            $isok = db('kepu')->where('id', $id)->update(['jia_status' => 1]);
            $arr = [];
            if ($isok) {
                $arr['code'] = 1;
            } else {
                $arr['code'] = 2;
            }
            return $arr;
        }
    }
    public function cg()
    {
        $id = input("id");
        $list = db('kepu')->alias('a')
            ->where('a.id', $id)
            ->field('a.*,b.filepath')
            ->join('attachment b', 'a.img = b.id')
            ->find();
        $this->assign('list', $list);
        if (request()->isAjax()) {
            $post = input('post.');
            if (empty($post['thumb'])) {
                $in = db('kepu')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            } else {
                $in = db('kepu')->where('id', $post['aid'])->update([
                    'title' => $post['title'],
                    'img' => $post['thumb'],
                    'content' => $post['content'],
                    'description' => $post['description']
                ]);
                if ($in) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
        return $this->fetch();
    }
    public function science()
    {
        $list = db('user')->where('integral', '<>', 0)->order('integral', 'desc')->paginate(10, false, ['query' => request()->param()]);
        $pag = $list->render();
        $this->assign('pag', $pag);
        $this->assign('list', $list);
        return $this->fetch();
    }
}
